﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Web;

namespace myking
{
    /// <summary>
    /// 记录日志文件到本地
    /// </summary>
    public class Log
    {
        private static readonly object obj = new object();
        /// <summary>
        /// 操作日志
        /// </summary>
        /// <param name="s">日志内容</param>
        public static void WriteLog(string title, string content, string filePath)
        {
            WriteLogs(title, content, "操作日志",filePath);
        }
        /// <summary>
        /// 错误日志
        /// </summary>
        /// <param name="s">日志内容</param>
        public static void WriteError(string title, string content, string filePath)
        {
            WriteLogs(title, content, "错误日志",filePath);
        }

        public static void WriteLogs(string title, string content, string type, string filePath)
        {
            lock (obj)
            {
                string path = filePath +"\\" + DateTime.Now.ToString("yyyy-MM-dd");
                if (!string.IsNullOrEmpty(path))
                {
                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }
                    path = path + "\\" + title + ".txt";
                    //path = path + "\\" + title + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
                    if (!File.Exists(path))
                    {
                        FileStream fs = File.Create(path);
                        fs.Close();
                    }
                    if (File.Exists(path))
                    {
                        StreamWriter sw = new StreamWriter(path, true, System.Text.Encoding.Default);
                        //sw.WriteLine(DateTime.Now + " " + title);
                        //sw.WriteLine("日志类型:" + type);
                        //sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ff") + "-记录-" + content);
                        sw.WriteLine(content);
                        //sw.WriteLine("----------------------------------------");
                        sw.Close();
                    }
                }
            }
        }
    }
}
